using Microsoft.Xna.Framework;
using RpgXna.Model.Entities;
using RpgXna.Model.Entities.MainCharacter;
using RpgXna.Model.Helpers;

namespace RpgXna.Model.Interfaces.ContextMenu.InteractionBehaviors
{
    class DropContextMenuItem : ContextMenuItem
    {
        private readonly PickableObject _targetObject;
        private readonly Inventory.Inventory _inventory;
        private readonly EnvironmentRepository _environment;
        private readonly Character _mainCharacter;

        public DropContextMenuItem(PickableObject targetObject, Inventory.Inventory inventory, EnvironmentRepository environment, Character mainCharacter)
        {
            _targetObject = targetObject;
            _inventory = inventory;
            _environment = environment;
            _mainCharacter = mainCharacter;
            Action = InternalExecute;
            ItemName = "Lacher";
        }

        void InternalExecute()
        {
            //object positioning into the environment before transformation => -40 y margin to avoir collide after drop
            var p = _mainCharacter.Position + new Vector2(_mainCharacter.Origin.X, -40);

            //transformation of the object drop position according to character orientation
            var tpos = Transforms.TransformPoint(p, _mainCharacter.Position, _mainCharacter.Rotation,
                                                 _mainCharacter.Origin);
            
            //assignment of the final transformed drop position to the target object
            _targetObject.Position = tpos;
            
            //target object position update
            _targetObject.Update();

            //check if there is no collision with another environment object
            if (!_environment.CheckCollisions(_targetObject))
            {
                //if eveything ok, remove the object from the invetory
                _inventory.Remove(_targetObject);
                //and add it back into the environment
                _environment.Add(_targetObject);
            }

            //if there was some obstacles to the drop, no need to revert position as it's of no importance when it is in the inventory
            //next drop attempt will update again the position
        }
    }
}